package com.sxdx.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import com.sxdx.dao.LabDAO;
import com.sxdx.dao.LabinfoDAO;
import com.sxdx.dao.MydateDAO;
import com.sxdx.entity.Labinfo;
import com.sxdx.entity.Laboratory;
import com.sxdx.entity.Mydate;
import com.sxdx.service.LabInfoService;

@SuppressWarnings("all")
@Service("labInfoService")
public class LabInfoServiceImpl implements LabInfoService {
	private static Logger logger = LogManager.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
	@Resource(name = "labinfoDAO")
	private LabinfoDAO labinfoDAO;
	@Resource(name = "labDAO")
	private LabDAO labDAO;
	@Resource(name = "mydateDAO")
	private MydateDAO mydateDAO;

	public void createLabInfo(Laboratory laboratory) {
		String hql = "from Mydate m where m.dayofweek!=1 and m.dayofweek!=7";
		List<Mydate> mydates = mydateDAO.find(hql);
		Labinfo labinfo = null;
		for (int i = 0; i < mydates.size(); i++) {
			for (int j = 1; j <= 4; j++) {
				labinfo = new Labinfo();
				labinfo.setLiLabName(laboratory.getLabName());
				labinfo.setLiODate(mydates.get(i).getDate());
				labinfo.setLiOWeek(mydates.get(i).getMyweek());
				labinfo.setLiContain(laboratory.getLabContain());
				labinfo.setLiMargin(laboratory.getLabContain());
				labinfo.setLiReserve(laboratory.getReserve());
				labinfo.setLiOType(j);
				labinfoDAO.save(labinfo);
			}
		}
	}

	public List<Labinfo> labInfo(String date, String type) {
		String hql = "from Labinfo l where l.liODate=? and l.liOType=?";
		List<Object> param = new ArrayList<Object>();
		param.add(date);
		param.add(Integer.parseInt(type));
		List<Labinfo> li = labinfoDAO.find(hql, param);
		return li;
	}

	public void labinfodelete(String id) {
		Laboratory l = labDAO.get(Laboratory.class, Integer.parseInt(id));
		String hql = "from Labinfo l where l.liLabName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(l.getLabName());
		List<Labinfo> lis = labinfoDAO.find(hql, param);
		for (Labinfo labinfo : lis) {
			labinfoDAO.delete(labinfo);
		}
	}

	public Labinfo getLabinfo(int labid, String date, String type) {
		Laboratory l = labDAO.get(Laboratory.class, labid);
		String hql = "from Labinfo l where l.liLabName=? and l.liODate=? and l.liOType=?";
		List<Object> param = new ArrayList<Object>();
		param.add(l.getLabName());
		param.add(date);
		param.add(Integer.parseInt(type));
		return labinfoDAO.find(hql, param).get(0);
	}

	public void updatelabinfo(Labinfo labinfo) {
		labinfoDAO.update(labinfo);
	}

	public void changereserveclose(int labid) {
		String labName = labDAO.get(Laboratory.class, labid).getLabName();
		String hql = "from Labinfo l where l.liLabName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(labName);
		List<Labinfo> l = labinfoDAO.find(hql, param);
		for (Labinfo labinfo : l) {
			labinfo.setLiReserve("0");
			labinfoDAO.update(labinfo);
		}
	}

	public void changereserveopen(int labid) {
		String labName = labDAO.get(Laboratory.class, labid).getLabName();
		String hql = "from Labinfo l where l.liLabName=?";
		List<Object> param = new ArrayList<Object>();
		param.add(labName);
		List<Labinfo> l = labinfoDAO.find(hql, param);
		for (Labinfo labinfo : l) {
			labinfo.setLiReserve("1");
			labinfoDAO.update(labinfo);
		}
	}
}
